c# winform程序读写ini配置文件 您所在的位置:网站首页 ini 文件读取 c# winform程序读写ini配置文件

c# winform程序读写ini配置文件

2023-09-07 15:19| 来源: 网络整理| 查看: 265

C# winform中读写ini文件

https://blog.csdn.net/source0573/article/details/49668079

c# winform程序读写ini配置文件

https://www.cnblogs.com/falcon-fei/p/9849868.html

背景:本来准备使用settings.settings来保存数据库配置等信息的。但是设置为“应用程序”级别,就没法方便的用 Properties.Settings.Default.Save() 更改保存。 且没法在程序不重启的情况下应用修改后的配置。如果设置为“用户”级别,就存在配置信息保存到appData文件夹下被清空缓存失效的风险。因此回过头来选择ini来保存这些配置信息

网上找了个ini读写操作类,直接copy拿来用,测试可用,代码如下:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace data_migration_desk.utils { // INI文件操作类 class IniFileHelper { string strIniFilePath; // ini配置文件路径 // 返回0表示失败,非0为成功 [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 返回取得字符串缓冲区的长度 [DllImport("kernel32", CharSet = CharSet.Auto)] private static extern long GetPrivateProfileString(string section, string key, string strDefault, StringBuilder retVal, int size, string filePath); [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetPrivateProfileInt(string section, string key, int nDefault, string filePath); /// /// 无参构造函数 /// /// public IniFileHelper() { this.strIniFilePath = Directory.GetCurrentDirectory() + "\\Properties\\sysconfig.ini"; } /// /// 有参构造函数 /// /// ini配置文件路径 /// public IniFileHelper(string strIniFilePath) { if (strIniFilePath != null) { this.strIniFilePath = strIniFilePath; } } /// /// 获取ini配置文件中的字符串 /// /// 节名 /// 键名 /// 默认值 /// 结果缓冲区 /// 结果缓冲区大小 /// 成功true,失败false public bool GetIniString(string section, string key, string strDefault, StringBuilder retVal, int size) { long liRet = GetPrivateProfileString(section, key, strDefault, retVal, size, strIniFilePath); return (liRet >= 1); } //或者!!! public string GetIniString(string section, string key) { StringBuilder temp = new StringBuilder(255); long i = GetPrivateProfileString(section, key, "", temp, 255, strIniFilePath); return temp.ToString(); } /// /// 获取ini配置文件中的整型值 /// /// 节名 /// 键名 /// 默认值 /// public int GetIniInt(string section, string key, int nDefault) { return GetPrivateProfileInt(section, key, nDefault, strIniFilePath); } /// /// 往ini配置文件写入字符串 /// /// 节名 /// 键名 /// 要写入的字符串 /// 成功true,失败false public bool WriteIniString(string section, string key, string val) { long liRet = WritePrivateProfileString(section, key, val, strIniFilePath); return (liRet != 0); } /// /// 往ini配置文件写入整型数据 /// /// 节名 /// 键名 /// 要写入的数据 /// 成功true,失败false public bool WriteIniInt(string section, string key, int val) { return WriteIniString(section, key, val.ToString()); } } }

使用方法:

读取:

IniFileHelper iniFileHelper = new IniFileHelper(); StringBuilder sb = new StringBuilder(60); iniFileHelper.GetIniString("tokeninfo", "apiUrl", "", sb, sb.Capacity); string aaa = sb.ToString();

写入:

IniFileHelper iniFileHelper = new IniFileHelper(); iniFileHelper.WriteIniString("tokeninfo", "apiUrl", "newString");

 

其中,ini配置文件有一些需要讲究的地方,不然,就会出现读取不到值的情况

 

C# winform中读写ini文件

INI文件是一种具有特定结构的文本文件,它的构成分为三部分,结构如下:

[Section1] key 1 = value2 key 1 = value2 …… [Section2] key 1 = value1 key 2 = value2 ……

文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value)。Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作,他们位于kernel32.dll下。

但是令人遗憾的是C#所使用的.NET框架下的公共类库并没有提供直接操作INI文件的类,所以唯一比较理想的方法就是调用API函数。

然后,.Net框架下的类库是基于托管代码的,而API函数是基于非托管代码的,(在运行库的控制下执行的代码称作托管代码。相反,在运行库之外运行的代码称作非托管代码。)如何实现托管代码与非托管代码之间的操作呢?.Net框架的System.Runtime.InteropServices命名空间下提供各种各样支持COM interop及平台调用服务的成员,其中最重要的属性之一DllImportAttribute可以用来定义用于访问非托管API的平台调用方法,它提供了对从非托管DLL导出的函数进行调用所必需的信息。下面就来看一下如何实现C#与API函数的互操作。

读操作:

[ DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); section:要读取的段落名 key: 要读取的键 defVal: 读取异常的情况下的缺省值 retVal: key所对应的值,如果该key不存在则返回空值 size: 值允许的大小 filePath: INI文件的完整路径和文件名 写操作:

[ DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); section: 要写入的段落名 key: 要写入的键,如果该key存在则覆盖写入 val: key所对应的值 filePath: INI文件的完整路径和文件名 这样,在就可以使用对他们的调用,用常规的方式定义一个名为 IniFile 类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.IO; namespace 测试 { /// /// INI文件的操作类 /// public class IniFile { public string Path; public IniFile(string path) { this.Path = path; } #region 声明读写INI文件的API函数 [ DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); [ DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, StringBuilder retVal, int size, string filePath); [ DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath); #endregion /// /// 写INI文件 /// /// 段落 /// 键 /// 值 public void IniWriteValue(string section, string key, string iValue) { WritePrivateProfileString(section, key, iValue, this.Path); } /// /// 读取INI文件 /// /// 段落 /// 键 /// 返回的键值 public string IniReadValue(string section, string key) { StringBuilder temp = new StringBuilder( 255); int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path); return temp.ToString(); } /// /// 读取INI文件 /// /// 段,格式[] /// 键 /// 返回byte类型的section组或键值组 public byte[] IniReadValues(string section, string key) { byte[] temp = new byte[ 255]; int i = GetPrivateProfileString(section, key, "", temp, 255, this.Path); return temp; } } } 程序在调用IniFile类时需要先实例化

string path = Environment.CurrentDirectory + @"\Config.ini"; //指定ini文件的路径 IniFile ini = new IniFile(path); string FpOrderNumber = ini.IniReadValue( "SQL", "FpOrderNumber"); //读取ini文件中键FpOrderNumber的值ini.IniWriteValue("SQL", "FpOrderNumber", tsslMo.Text);//将tsslMo.Text的值写到键FpOrderNumber中去



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有